function myChart(data, element, chartType) {
var ctx = document.getElementById(element).getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: {
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
});
}
document.getElementById("reset").onclick=function(){
	$("#start_date").val("")
	$("#end_date").val("")
}
document.getElementById("q").onclick=function(){
	$("#all>hr").remove()
	$("#all>canvas").remove()
	$("#all>center").remove()
	$("#all>table").remove()
	var data={}
	data.start_date=$("#start_date").val()
	data.end_date=$("#end_date").val()
	if (data.start_date==="" && data.end_date===""){
		showErrorInfo("请选择一个日期！")
		return false;
	}
	Q(data)
}
function Q(data){
	data=JSON.stringify(data)
	$.ajax({
		"url":"/casher_query/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"data":data},
		"dataType":"jsonp",
		"success":function(data){
			if(responseCheck(data)){
			

				//日期显示
				var d1=$("#start_date").val()
				var d2=$("#end_date").val()
				if (d1==="" && d2===""){
					$("#t1").text(data.today+" 至 现在")
				}
				else{
					$("#t1").text(d1+" 至 "+d2)
				}
				//日期显示
				var all=document.getElementById("all")
				var color=["green","#da4748","#c6bb40","#318ccc","#8835c8","black"]
				var i=-1
				for (var key in data.content){
					if(key==="各加油站会员消费统计图2"){
						continue
					}
					i+=1
					var T={
					"labels":[],
					"datasets":[
							{
							"label":"",
							"backgroundColor":color[i],
							"data":[],
							},
						]
					}
					if(key==="各加油站会员消费统计图"){
						T.datasets[0].label="优惠前消费金额"
						T.datasets.push(
							{
							"label":"优惠后消费金额",
							"backgroundColor":"red",
							"data":[],
							},
						)
					}
					var center=document.createElement("center")
					center.style.fontSize="200%"
					center.textContent=key
					all.appendChild(center)
					var canvas=document.createElement("canvas")
					canvas.setAttribute("id",key)
					all.appendChild(canvas)
					var tab=document.createElement("table")
					tab.className="table table-hover table-striped  table-bordered  table-condensed  table-responsive "
					tab.style.width="100%"
					tab.style.marginTop="20px"
					tab.style.border="1px solid "
					var thead=document.createElement("thead")
					for(var k2 in data.content[key]){
						var th=document.createElement("th")
						th.textContent=k2
						th.style.border="1px solid "
						thead.appendChild(th)
						T.labels.push(k2)
						if(key==="各加油站会员消费统计图"){
							T.datasets[0].data.push( data.content["各加油站会员消费统计图"][k2] )
							T.datasets[1].data.push( data.content["各加油站会员消费统计图2"][k2] )
						}
						else{
							T.datasets[0].data.push(data.content[key][k2])
						}
					}
					tab.appendChild(thead)
					var tbody=document.createElement("tbody")
					for(var site in data.content[key]){
						var td=document.createElement("td")
						td.textContent=data.content[key][site]
						td.style.border="1px solid"
						tbody.appendChild(td)
					}
					tab.appendChild(tbody)
					all.appendChild(tab)
					var hr=document.createElement("hr")
					all.appendChild(hr)
					myChart(T,key)
					//表格部分
				}
			}
		}
	})
}
Q({"start_date":"today","end_date":""})
